package com.android.gufengmall.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.CultureAdapter;
import com.android.gufengmall.bean.CultureBean;
import com.android.gufengmall.gen.CultureBeanDao;
import com.android.gufengmall.gen.DaoSession;

import java.util.ArrayList;
import java.util.List;

/**
 * 文学工艺知识专区
 */
public class literatureFragment extends Fragment implements CultureAdapter.CommentListener {

    private static final String TAG = "literatureFragment";

    private RecyclerView mRvLiter;

    private ArrayList<CultureBean> mCultures;
    private CultureAdapter mCultureAdapter;
    private App mMyApp;
    private DaoSession mDaoSession;
    private CultureBeanDao mCultureBeanDao;

    public literatureFragment() {
    }

    public static literatureFragment newInstance() {
        return new literatureFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_literature, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        mRvLiter = view.findViewById(R.id.rv_liter);
    }

    private void initData() {
        mCultures = new ArrayList<>();

        mMyApp = (App) getActivity().getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mCultureBeanDao = mDaoSession.getCultureBeanDao();
        List<CultureBean> cultureBeans = mCultureBeanDao.loadAll();
        mCultures.addAll(cultureBeans);

        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRvLiter.setLayoutManager(layoutManager);

        mCultureAdapter = new CultureAdapter(requireContext(), mCultures);
        mCultureAdapter.setOnCommentListener(this);
        mRvLiter.setAdapter(mCultureAdapter);
    }

    @Override
    public void onLikeListener(int position) {
        CultureBean cultureBean = mCultureBeanDao.loadByRowId(position);
        boolean isLike = cultureBean.getIsLike();
        cultureBean.setIsLike(!isLike);
        mCultureBeanDao.update(cultureBean);
        if (isLike) {
            Toast.makeText(requireContext(), "取消点赞", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "点赞成功", Toast.LENGTH_SHORT).show();
        }
        mCultureAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCollectListener(int position) {
        CultureBean cultureBean = mCultureBeanDao.loadByRowId(position);
        boolean isCollect = cultureBean.getIsCollect();
        cultureBean.setIsCollect(!isCollect);
        mCultureBeanDao.update(cultureBean);
        if (isCollect) {
            Toast.makeText(requireContext(), "取消收藏", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(requireContext(), "收藏成功", Toast.LENGTH_SHORT).show();
        }
        mCultureAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemSelectListener(String position) {
        Intent intent = new Intent(requireContext(), CommentActivity.class);
        intent.putExtra("status", 0);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}